package com.cl.core.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.cl.core.dto.PagingReqDTO;
import com.cl.core.mapper.PlatformRolesMapper;
import com.cl.core.pojo.PlatformRoles;
import com.cl.core.service.RoleServices;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.beanutils.ConvertUtils;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

/**
 * @author liddhappy
 * @description menuServices 实现
 * @createTime 2022/12/3 21:05
 */
@Service
@Slf4j
public class RoleServiceImpl extends ServiceImpl<PlatformRolesMapper, PlatformRoles> implements RoleServices {

    @Autowired
    PlatformRolesMapper roleMapper;


    @Override
    public void addRole(PlatformRoles roles) {
        log.info("add Role " + roles);
        roleMapper.insertSelective(roles);
        log.info("add Role success");
    }

    @Override
    public void deleteRole(String roleId) {
        log.info("deleteRoleByUserId " + roleId);
        Long convert = !StringUtils.isEmpty(roleId) ? (Long) ConvertUtils.convert(roleId, Long.class) : Long.MIN_VALUE;
        roleMapper.deleteByPrimaryKey(convert);
        log.info("deleteRoleByUserId  success");
    }

    @Override
    public void updateRole(PlatformRoles role) {
        log.info("update Role " + role);
        PlatformRoles platformRoles = roleMapper.selectByPrimaryKey(role.getRoleId());
        BeanUtils.copyProperties(role, platformRoles);
        roleMapper.updateByPrimaryKey(platformRoles);
        log.info("update Role success");
    }

    @Override
    public PlatformRoles selectRole(String roleId) {
        log.info("select Role " + roleId);
        Long convert = !StringUtils.isEmpty(roleId) ? (Long) ConvertUtils.convert(roleId, Long.class) : Long.MIN_VALUE;
        PlatformRoles platformRoles = roleMapper.selectByPrimaryKey(convert);
        log.info("selectUser  result  " + platformRoles);
        log.info("selectUser  success");
        return platformRoles;
    }

    @Override
    public IPage<PlatformRoles> paging(PagingReqDTO<PlatformRoles> reqDTO) {

        //创建分页对象
        Page page = new Page(reqDTO.getCurrent(), reqDTO.getSize());
        //转换结果
        return baseMapper.paging(page, reqDTO.getParams());
    }
}

